import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.List;
import java.util.Map;

/**
 * @author <a href='mailto:maruichao52@gmail.com'>MRC</a>
 * @since 2021/1/6
 */
@Slf4j
public class UrlTest {

    /**
     * <p>
     * <p>author: <a href='mailto:maruichao52@gmail.com'>MRC</a>
     * 构造一个统一资源标识符
     * http://root@baidu.com:80/default.html?key=1#desc
     * URI uri = new URI("http", "baidu.com", "/test.html", null);
     *
     * @return void
     * @since 2021/1/6
     **/
    @Test
    public void createURI() throws URISyntaxException {
        URI uri = new URI("http", "root:123456", "baidu.com", 80, "/default.html", "key=1", "desc");
        log.info(uri.toString());
    }

    /**
     * <p>通过URL 打开一个链接，获得流内容，转换为字符串
     * <p>author: <a href='mailto:maruichao52@gmail.com'>MRC</a>
     *
     * @return void
     * @since 2021/1/6
     **/
    @Test
    public void createURL() throws IOException {

        URL url = new URL("http", "localhost", "/api/header/get");
        log.info(url.toString());

        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream inputStream = connection.getInputStream();

        byte bytes[] = new byte[1024];
        int index = 0;

        while ((index = inputStream.read(bytes)) != -1) {
            log.info("input read string={}", new String(bytes));
        }
    }

    @Test
    public void getUrlName() throws Exception {

        URL url = new URL("http://192.168.13.111:85/CIM2_V3/page/commonUtilsForGet/getFile?goodsFileCode=102&fileName=FILEA_2020120317302067439414.jpg");
        System.out.printf(url.getFile());
    }

    @Test
    public void createPOST() throws Exception {
        URL url = new URL("http", "localhost", "/api/header/post");
        log.info(url.toString());

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);

        String json = "{\"name\":\"desc\",\"age\":12}";
        // 写入请求头
        connection.setRequestProperty("Accept", "*/*");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length", json.length() + "");

        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(json.getBytes());
        outputStream.flush();

        // 打开链接
        connection.connect();
        log.info("response code={}", connection.getResponseCode());

        Map<String, List<String>> fields = connection.getHeaderFields();

        for (String key : fields.keySet()) {
            log.info("response header key={} value={}", key, fields.get(key));
        }

        InputStream inputStream = connection.getInputStream();
        byte bytes[] = new byte[1024];

        while (inputStream.read(bytes) != -1) {
            log.info("response data={}", new String(bytes));
        }
    }
}
